import { Button, Tooltip, InputNumber, Input, Form, message } from 'antd';
import { getColorTextLoadMap, onMouseOutBtn, removeDateHmsStr, renderTableText, tableSorter, tooltipLength, priceFormat } from '@/utils';
import { PlusCircleOutlined, DeleteOutlined, AppstoreAddOutlined } from '@ant-design/icons';
import { getLoadMapList, getSelectDataMapList, getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { findObjectById } from '@/utils/findObjectById';
import pageStyles from '@/common/pageLayout.less';
import { RegularExpression } from '@/common/constVariable';
import { DictionaryRenderFun, formatDecimal, formatDecimalFour, getColorTextDoms } from '../../Acommon';

/** 材料表格 */
const openMaterielColumns = (openMaterialModalStatus, delSelectRow, assetTypeList, pageType, changeValue) => {
  const disabled: boolean = pageType === 'detail'; // 是否是查看
  let tableConfig: Record<string, any>[] = [
    {
      title: '材料名称',
      dataIndex: 'assetName',
      key: 'assetName',
      width: 110,
      fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetName'),
      render: (text: string) => renderTableText(text, true),
    },
    // {
    //   title: '材料编码',
    //   dataIndex: 'materialCode',
    //   key: 'materialCode',
    //   width: 110,
    //   fixed: 'left',
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'materialCode'),
    //   render: (text: string) => renderTableText(text, true),
    // },
    {
      title: '材料分类',
      dataIndex: 'assetClassName',
      key: 'assetClassName',
      width: 110,
      fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetClassName'),
      render: (text: number) => DictionaryRenderFun({ text, isState: false }),
    },
    {
      title: '品牌',
      dataIndex: 'brandType',
      key: 'brandType',
      width: 120,
      sorter: (a: any, b: any) => tableSorter(a, b, 'brandType'),
      render: (text: string) => DictionaryRenderFun({ text, isState: false }),
    },
    {
      title: '规格型号',
      dataIndex: 'paramsInfo',
      key: 'paramsInfo',
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'paramsInfo'),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '计量单位',
      dataIndex: 'unitOfMeasurementName',
      key: 'unitOfMeasurementName',
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'unitOfMeasurementName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '单价（元）',
      dataIndex: 'assetValue',
      key: 'assetValue',
      width: 120,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetValue'),
      render: (text: number | null) => formatDecimalFour(text),
    },
    {
      title: '数量',
      dataIndex: 'residueNum',
      key: 'residueNum',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'residueNum'),
      render: (text: number | null) => formatDecimal(text),
    },
    {
      title: '总价（元）',
      dataIndex: 'totalValue',
      key: 'totalValue',
      width: 120,
      sorter: (a: any, b: any) => tableSorter(a, b, 'totalValue'),
      render: (text: number | null) => formatDecimalFour(text),
    },
    // {
    //   title: '取得日期',
    //   dataIndex: 'enterStoreTime',
    //   key: 'enterStoreTime',
    //   width: 150,
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'enterStoreTime'),
    //   render: (text: string) => removeDateHmsStr(text) || '--',
    // },
    // {
    //   title: '使用期限',
    //   dataIndex: 'durableYears',
    //   key: 'durableYears',
    //   width: 120,
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'durableYears'),
    //   render: (text: string) => {
    //     return (
    //       <Tooltip title={text}>
    //         <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
    //       </Tooltip>
    //     );
    //   },
    // },
    {
      title: '有效数量',
      dataIndex: 'validNum',
      key: 'validNum',
      width: 160,
      fixed: 'right',
      sorter: (a: any, b: any) => tableSorter(a, b, 'validNum'),
      render: (text: number | null) => formatDecimal(text),
    },
  ];

  if (!disabled) {
    tableConfig?.push({
      title: (
        <div style={{ width: '100%', textAlign: 'center' }}>
          <Button
            type="link"
            size="small"
            icon={<AppstoreAddOutlined />}
            className="manageBtntTxtStyle"
            onMouseOut={onMouseOutBtn}
            onClick={openMaterialModalStatus}
          >
            选择
          </Button>
        </div>
      ),
      dataIndex: 'otherBtn',
      key: 'otherBtn',
      visible: true,
      width: 100,
      fixed: 'right',
      render: (_text: any, record: any) => {
        return (
          <div className="counterpoartDelBtnStyle">
            <Button
              type="link"
              size="small"
              icon={<DeleteOutlined />}
              className={'manageBtntTxtStyle'}
              onMouseOut={onMouseOutBtn}
              onClick={() => delSelectRow(record)}
            >
              删除
            </Button>
          </div>
        );
      },
    });
  }
  return tableConfig;
};

/** 表格配置 */
const materialTableConfig = (assetTypeList, changeNum) => {
  return [
    {
      title: '材料名称',
      dataIndex: 'assetName',
      key: 'assetName',
      width: 110,
      fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetName'),
      render: (text: string) => renderTableText(text, true),
    },
    // {
    //   title: '材料编码',
    //   dataIndex: 'materialCode',
    //   key: 'materialCode',
    //   width: 110,
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'materialCode'),
    //   render: (text: string) => renderTableText(text, true),
    // },
    {
      title: '材料分类',
      dataIndex: 'assetClassName',
      key: 'assetClassName',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetClassName'),
      render: (text: number) => DictionaryRenderFun({ text, isState: false }),
    },
    {
      title: '规格型号',
      dataIndex: 'paramsInfo',
      key: 'paramsInfo',
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'paramsInfo'),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '计量单位',
      dataIndex: 'unitOfMeasurementName',
      key: 'unitOfMeasurementName',
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'unitOfMeasurementName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '单价（元）',
      dataIndex: 'assetValue',
      key: 'assetValue',
      width: 130,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetValue'),
      render: (text: number | null) => formatDecimalFour(text),
    },
    {
      title: '数量',
      dataIndex: 'residueNum',
      key: 'residueNum',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'residueNum'),
      render: (text: number | null) => formatDecimal(text),
    },
    {
      title: '总价（元）',
      dataIndex: 'totalValue',
      key: 'totalValue',
      width: 130,
      sorter: (a: any, b: any) => tableSorter(a, b, 'totalValue'),
      render: (text: number | null) => formatDecimalFour(text),
    },
    {
      title: '品牌',
      dataIndex: 'brandType',
      key: 'brandType',
      width: 120,
      sorter: (a: any, b: any) => tableSorter(a, b, 'brandType'),
      render: (text: string) => DictionaryRenderFun({ text, isState: false }),
    },
    {
      title: '取得日期',
      dataIndex: 'enterStoreTime',
      key: 'enterStoreTime',
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'enterStoreTime'),
      render: (text: string) => removeDateHmsStr(text) || '--',
    },
    {
      title: '使用期限',
      dataIndex: 'durableYears',
      key: 'durableYears',
      width: 120,
      sorter: (a: any, b: any) => tableSorter(a, b, 'durableYears'),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '有效数量',
      dataIndex: 'validNum',
      key: 'validNum',
      width: 160,
      fixed: 'right',
      sorter: (a: any, b: any) => tableSorter(a, b, 'validNum'),
      render: (text: number | string, record: Record<string, any>) => {
        const MaxNum: number = Number(record?.residueNum || 0);
        // 控制当数量大于当前可选数量时显示
        let ishowMax = Number(text) > MaxNum ? true : false;
        let ishowMin = Number(text) <= 0;
        if (MaxNum <= 0) {
          return <div style={{ textAlign: 'center', padding: '0 11px' }}>暂无材料数量</div>;
        } else {
          return (
            <>
              <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
                <InputNumber
                  status={ishowMax || ishowMin ? 'error' : ''}
                  bordered={ishowMax || ishowMin ? true : false}
                  value={text}
                  style={{ width: '100%' }}
                  onChange={(value: any) => {
                    if (Number(value) > Number(record.residueNum)) {
                      message.error('材料数量不足！');
                      return;
                    } else {
                      changeNum(record.id, value);
                    }
                  }}
                  min={1}
                  max={MaxNum}
                  disabled={text == 0}
                />
              </div>
              {ishowMax && <div style={{ paddingLeft: '5px', color: 'red' }}>{`最多可选择的数量为${MaxNum}`}</div>}
              {ishowMin && <div style={{ paddingLeft: '5px', color: 'red' }}>{`最少选择的数量为1`}</div>}
            </>
          );
        }
      },
      // render: (text: string | number, record: Record<string, any>) => {
      //   return (
      //     <InputNumber
      //       name="validNum"
      //       bordered={false}
      //       value={record.validNum}
      //       onChange={(value) => {
      //         if(Number(value) > Number(record.residueNum)){
      //           message.error('材料材料数量不足！');
      //           return;
      //         } else {
      //           changeNum(record.id, value);
      //         }
      //       }}
      //     ></InputNumber>
      //   );
      // },
    },
  ];
};

export { materialTableConfig, openMaterielColumns };
